﻿using System.Web;
using TimeMachine.Engine.Agents;
using WebMachine.Controllers;

namespace WebMachine
{
	public class DownloadData : IHttpHandler
	{
		private readonly IProjectSettingsStore store = new WebMachineProjectsSettingsStore();

		public void ProcessRequest(HttpContext context)
		{
			string projectName = context.Request.QueryString["project"];
			if (store.HasProjectSettings(projectName))
			{
				context.Response.ContentType = "text/csv";
				context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + projectName + ".csv\"");
				context.Response.Write(new DownloadDataAgent(store.Get(projectName)).GetCsv());
			}
			else
			{
				context.Response.ContentType = "text/html";
				string message = projectName != null ? string.Format("Проект {0} не найден", projectName) : "Не указан проект";
				context.Response.Write(message);
			}
		}

		public bool IsReusable
		{
			get { return false; }
		}
	}
}